home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 12531 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.5 KB

  1. Path: keats.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: How can I use huge || very small number?
  5. Date: 1 Apr 1996 07:29:44 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4josp8INNmd2@keats.ugrad.cs.ubc.ca>
  8. References: <315681F3.314D@blue.nowcom.co.kr> <4joh0l$jch@news.acns.nwu.edu> <4joijj$m0h@news.fsu.edu> <4joj2c$jh2@news.acns.nwu.edu>
  9. NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
  10.  
  11. In article <4joj2c$jh2@news.acns.nwu.edu>,
  12. Usman Muzaffar <muzaffar@casbah.acns.nwu.edu> wrote:
  13.  >In article <4joijj$m0h@news.fsu.edu>,
  14.  >Justin C Lloyd <lloyd@upsilon.cs.fsu.edu> wrote:
  15.  >>On 1 Apr 1996 12:08:53 GMT, Usman Muzaffar (muzaffar@casbah.acns.nwu.edu)
  16.  >>wrote:
  17.  >>** In article <315681F3.314D@blue.nowcom.co.kr>,
  18.  >>** whoever  <whatever@blue.nowcom.co.kr> wrote:
  19.  >>** >may be it is FAQ but...
  20.  >>** >how can I compute 10000! or 0.12345......
  21.  >>
  22.  >>** 10000! is a very, very big number.
  23.  >>** I'm not sure it's even representable by standard IEEE fp notation.
  24.  >>** Any have that formula? 2*pi*e something or another.
  25.  >>
  26.  >>
  27.  >>I'm sorry that I don't remember the location (possibly sunsite.unc.edu?),
  28.  >>but there was a collection of C snippets/programs that contained a program
  29.  >>that "calculated" huge factorials by using strings.  I don't recall the
  30.  >>algorithm, it was quite complicated.  Anyone else know what I'm referring
  31.  >>to?
  32.  >>
  33.  >
  34.  >If it's what I'm thinking of, it's not complicated at all.
  35.  >Essentially, you teach the computer to do math the way we do, never doing 
  36.  >more than one digit at a time, and just writing lines and lines of numerals
  37.  >in neat rows. The advantage is in output: it's not that you couldn't write
  38.  >a bit of code that couldn't calculate 10000!, but getting it from binary
  39.  >to decimal output is as cumbersome as calculating the value itself.
  40.  >
  41.  >Of course, the down side is that it's very slow, inelegant, and (let's face it)
  42.  >kind of stupid to get a computer to add the way we do. After all, it's the
  43.  >very fact that they *don't* do things slowly is what first attracted us. :)
  44.  
  45. Actually the way computers add numbers is very much like ``the way we do it'',
  46. with some refinements (such as lookahead carry propagation to do more work in
  47. parallel). Multiplication is also done similarly, with all kinds of refinements
  48. to improve the use of parallel computation.
  49.  
  50. The nice thing is that in binary, the rules for addition simply boil down to
  51. logical operations. The possibilities for adding two binary digits and a carry
  52. are easily enumerated by logic, and implemented using logic gates.
  53.  
  54. The way primitive operations in multi-precision integer math libraries are
  55. implemented essentially mimics the way people do addition, multiplication or
  56. division. It's not stupid at all. If you want to do math with large integers,
  57. you have to implement these primitive operations. It is slower, but that's just
  58. the rule of TANSTAAFL at work.
  59.  
  60. The way you would implement an arithmetic operation on a large integer would
  61. not precisely mimic what you do on paper, of course. And you do not have to
  62. deal with one digit at a time. You deal with entire words at a time. For
  63. example, if your computer handles 16-bit words efficiently, you represent the
  64. large integers as arrays if 16-bit words, and do the computations in base
  65. 65536 rather than base 10, but the basics are still the same. You start with
  66. the rightmost words, add them, carry the 1 or 0, add the next ones with the
  67. carry, etc.
  68.  
  69.  
  70. -- 
  71.  
  72.